package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Menus;
import com.example.result.Result;
import com.example.service.MenuService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 权限控制层
 * @date 2020-01-09
 */
@RestController
@RequestMapping("/api")
public class MenuController {
    @Autowired
    MenuService menuService;

    // =============================权限管理===================================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加权限信息
     */
    @RequestMapping(value = "/menus", method = RequestMethod.POST)
    public Result addMenus(@RequestBody String json) {
        // 使用GSON将JSON数据转换为Role对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Menus menu = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Menus.class);
        return menuService.addMenus(menu);
    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新权限信息
     */
    @RequestMapping(value = "/menus", method = RequestMethod.PUT)
    public Result updateMenus(@RequestBody String json) {
        // 使用GSON将JSON数据转换为Role对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Menus menu = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Menus.class);
        return menuService.updateMenus(menu);
    }

    /**
     * @param menuId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据编号权限角色信息
     */
    @RequestMapping(value = "/menus/{menuId}", method = RequestMethod.DELETE)
    public Result deleteMenuById(@PathVariable("menuId") String menuId) {
        return menuService.deleteMenuById(menuId);
    }

    /**
     * @return 以JSON形式返回所有权限信息
     * @describe 查询所有权限信息
     */
    @RequestMapping(value = "/menus", method = RequestMethod.GET)
    public Result queryAllMenus() {
        return menuService.queryAllMenus();
    }

    /**
     * @param menuId
     * @return 以JSON形式返回该权限信息
     * @describe 根据编号查询该权限信息
     */
    @RequestMapping(value = "/menus/{menuId}", method = RequestMethod.GET)
    public Result queryMenuById(@PathVariable("menuId") String menuId) {
        return menuService.queryMenuById(menuId);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加权限信息
     */
    @RequestMapping(value = "/menus/excels", method = RequestMethod.POST)
    public Result addMenuByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<Menus> list = JSON.parseArray(parseObject.getString("json"), Menus.class);

        System.out.println(list);

        return menuService.addMenuByExcel(list);

    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 权限信息模糊查询
     */
    @RequestMapping(value = "/menus/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return menuService.serachByInfo(info);

    }

    /**
     * @param menuIds
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据权限编号批量删除权限信息
     */
    @RequestMapping(value = "/menus/dels", method = RequestMethod.POST)
    public Result deleteMenuByIds(@RequestBody Map<String, String> menuIds) {

        return menuService.deleteMenuByIds(menuIds);
    }

}
